﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Apollo;
using NHibernate;
using NHibernate.Transform;
using System.Collections;

namespace Apollo.Website
{
    public partial class Controls_Dashboard_EmployeeAndPositionKPIs : System.Web.UI.UserControl
    {
        public Employee Employee { get; set; }
        public Position Position { get; set; }
        public IDictionary<string, KPI> positionKPI;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(Apollo.Application.CurrentUser is UserCommon)
                this.Employee = (Apollo.Application.CurrentUser as UserCommon).Employee;

            if (this.Employee != null)
                this.Position = this.Employee.Position;
            else
                this.Position = Apollo.Application.CurrentUser.Position;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            PopulateEmployeeKPIs();

            PopulatePositionKPIs();

            LiteralJS.Text = @"
        <script type=""text/javascript"">
            $(document).ready(function () {
                $(""#employee-position-kpi-tabs"").tabs();

            });
        </script>
";
        }

        protected void PopulateEmployeeKPIs()
        {
            if (this.Employee != null)
                LabelEmployee.Text = this.Employee.FullName + " [" + this.Employee.InternalCode + "]";
            else
                LabelEmployee.Text = "Angajat inexistent";
        }

        protected void PopulatePositionKPIs()
        {
            if (this.Position != null)
                LabelPosition.Text = this.Position.Name;
            else
                LabelPosition.Text = "AXA Life";

            string sqlSP = "exec [dbo].[sp_dashboard_position_details] ?, ?";

            IQuery sqlQuery = Apollo.Application.CurrentSession.CreateSQLQuery(sqlSP);
            /*
            sqlQuery.SetParameter(0, this.Position != null ? (object)this.Position.Id : null);
            sqlQuery.SetParameter(1, this.Employee != null && this.Employee.ID_Last_CommissionCalculation != null ? this.Employee.ID_Last_CommissionCalculation : null);
        
            IList kpis = sqlQuery.SetResultTransformer(Transformers.ToList).List();

            this.positionKPI = new Dictionary<string, KPI>();
            foreach (object obj in kpis) {
                KPI kpi = new KPI(obj);

                if (!string.IsNullOrEmpty(kpi.Name))
                    this.positionKPI.Add(kpi.Name, kpi);
            }

            LabelNoOfEmployees.Text = this.GetPositionKPI("NoOfEmployees");
            LabelNoOfContracts.Text = this.GetPositionKPI("NoOfContracts");
            LabelNoOfContractsNB.Text = this.GetPositionKPI("NoOfContractsNB");

            LabelCommissionCalculationNoOfContracts.Text = this.GetPositionKPI("CCNoOfContracts");
            LabelLastCommissionCalculationDate.Text = this.GetPositionKPI("CCCalculationDate");
            LabelCommissionCalculationStartDate.Text = this.GetPositionKPI("CCStartDate");
            LabelCommissionCalculationEndDate.Text = this.GetPositionKPI("CCEndDate");
            */
        }

        protected string GetPositionKPI(string kpiName)
        {
            if (positionKPI.ContainsKey(kpiName))
            {
                KPI kpi = positionKPI[kpiName];

                if (kpi.IntValue != null)
                    return kpi.IntValue.ToString();
                else if (kpi.DateValue != null)
                    return kpi.DateValue.Value.ToString("dd/MM/yyyy");
            }

            return "-";
        }

        public class KPI
        {
            public KPI(object kpiBase)
            {
                if (kpiBase is IList)
                {
                    try
                    {
                        this.Name = (kpiBase as IList)[0] as string;

                        if ((kpiBase as IList)[1] != null)
                            this.IntValue = int.Parse((kpiBase as IList)[1].ToString());

                        if ((kpiBase as IList)[2] != null)
                            this.DateValue = DateTime.Parse((kpiBase as IList)[2].ToString());
                    }
                    catch { }
                }
            }
            public virtual string Name { get; set; }
            public virtual int? IntValue { get; set; }
            public virtual DateTime? DateValue { get; set; }
        }
    }
}